package com.XinYun.Library.modules.Register.Servlet;

import com.XinYun.Library.modules.Register.Dao.Impl.UserDaoImpl;
import com.XinYun.Library.modules.commons.ResponseWrapper.ResponseWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileStore;

@WebServlet(name = "VerifyServlet",value = "/VerifyServlet")
public class VerifyServlet extends HttpServlet {
    UserDaoImpl userDao = new UserDaoImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader reader = request.getReader();
        String readerLine = reader.readLine();
        JSONObject jsonObject = JSON.parseObject(readerLine);
        String code = jsonObject.getString("Verify_code");
        String username = jsonObject.getString("Verify_name");
        String direction = jsonObject.getString("Verify_direction");
        String date = jsonObject.getString("Verify_date");

        HttpSession session = request.getSession();

        try {
            if(userDao.isCode(code) && userDao.isVerify(username, direction, date)) {
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.NoData();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //response.sendRedirect("Register.jsp");
            }else if(!userDao.isCode(code)){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.InviteFail();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Verify.jsp").forward(request,response);
            }else {
                try {
                    if (userDao.isCode(code) && !userDao.isVerify(username, direction, date)){
                        ResponseWrapper responseWrapper = null;
                        responseWrapper = ResponseWrapper.MemberFail();
                        String s = JSON.toJSONString(responseWrapper);
                        PrintWriter writer = response.getWriter();
                        writer.println(s);
                        System.out.println(s);
                        //request.getRequestDispatcher("Verify.jsp").forward(request,response);
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

